package com.wthink.card.models;

import java.nio.ByteBuffer;

public class ShdFrameBundle {
	
	//frame type define by lenz_shd
	    public static final int M_TYPE_MAINBOARD = 0x0a;
	    public static final int M_TYPE_GPSPROC = 0xfe;
	    public static final int M_TYPE_SHEDULE = 0xff;
	    
	    //SendToMainBoardNormalFrm
	    public static final int CFS_DRVDUTY					= 0x23;
	    public static final int CFS_TTS_VOICE				= 0x36;
	    public static final int CFS_TTS_READ				= 0x44;		//
	    public static final int CFS_VIDEO_CHN_CTRL			= 0x7c;		//(0x7c)
	    public static final int CFS_KPLINE					= 0x8d;
	    
	    //from gpsmainboard	 
	    public static final int S_TYPE_FT_RECVGPS			= 0xa0;
	    public static final int S_TYPE_FT_CUT_AUDIO			= 0x05;		//
	    
	    //
	    public static final int SB_DNLD_FRM_START_BIT = 0x02;
		public static final int SB_DNLD_FRM_END_BIT = 0x17;
		public static final int SB_DNLD_FRM_LAST_BIT = 0x03;
		public static final int SB_DNLD_FRM_RESP_SUCC = 0x05;
		public static final int SB_DNLD_FRM_RESP_FAIL = 0x07;
		public static final int SB_DNLD_FRM_RESP_PEERSTOP = 0x08;
	
	// frame state for parser
		public static final int FS_NOT_READY = 0;  // FRAME body wasn't fullfilled
		public static final int FS_BODY_READY = 1;		// frame body is fullfilled , it's ready for process
		public static final int FS_FRM_READY = 2;
		public static final int FS_PROCESSED = 3;	// This msg is processed..
		
		//scoket
		/** **/
		public static final int A_SOCKET_MODEL_01		= 0x01;
		/** **/
		public static final int A_SOCKET_ICCARD_02		= 0x02;
		/** **/
		public static final int A_SOCKET_DATA_03		= 0x03;
		
		/*  */
		public static final int B_SOCKET_FRAME_01		= 0x01;
		public static final int B_SOCKET_FRAME_02		= 0x02;
		public static final int B_SOCKET_FRAME_03		= 0x03;
		public static final int B_SOCKET_FRAME_04		= 0x04;
		public static final int B_SOCKET_FRAME_05		= 0x05;


		public int		frameBundleState;
		public int		frameBundleBodyCurrLen = 0;

		public int		useExternalBuffer = 0;  // save frame body to external buffer .......
		
		public int	frameBundleSubType = 0;
		public int 	frameBundleMainType = 0;
		public int	frameBundleBodyLen = 0;
		public int 	frameBundleChkSum = 0 ;
		public byte[]	frameBundleBody;

		public ShdFrameBundle(){
			frameBundleState = 0;
		}
		
		public ShdFrameBundle(int type){
			frameBundleState = 0;
			frameBundleSubType = type;
		}

		public int	getFrameBundleState(){
			return frameBundleState;
		}

		public void setFrameBundleState(int state){
			frameBundleState = state;
		}

		public int getFrameBundleSubType(){
			return frameBundleSubType;
		}
		
		public int getFrameBundleMainType(){
			return frameBundleMainType;
		}
		
		public int getFrameBundleChkSum(){
			return frameBundleChkSum;
		}

		public void setFrameBundleSubType(int type){
			frameBundleSubType = type;
		}
		
		public void setFrameBundleMainType(int type){
			frameBundleMainType = type;
		}
		
		public void setFrameBundleChkSum(int val){
			frameBundleChkSum = val;
		}
		public int getFrameBundleBodyLen(){
			return frameBundleBodyLen;
		}

		public void setFrameBundleBodyLen(int len){
			frameBundleBodyLen = len;
		}	

		public byte[] getFrameBundleBody(){
			return frameBundleBody;
		}

		public static boolean isValidFrameBundle(int fType){
			return true;
		}
		
		public boolean isGpsmainboardFrameBundle(){
			if(frameBundleMainType == M_TYPE_MAINBOARD) return true;
			return false;
		}
		
		public boolean isGpsprocFrameBundle(){
			if(frameBundleMainType == M_TYPE_GPSPROC) return true;
			return false;
		}
		
		public int  getFrameBundleBodyLeft(){
			return (frameBundleBodyLen - frameBundleBodyCurrLen);
		}

		public void prepareFrameBundleBody(){
			if(frameBundleBody == null && (useExternalBuffer == 0)){
				frameBundleBody = new byte[frameBundleBodyLen];
			}	
		}

		//-----------------------------------------
		//  maxdatalen : max data left in <src> to read
		//
		public int fillFrameBundleBody(ByteBuffer src,int maxdatalen){
			int bodyDemandLen = frameBundleBodyLen - frameBundleBodyCurrLen;

			if(maxdatalen >= bodyDemandLen){
				src.get(frameBundleBody,frameBundleBodyCurrLen,bodyDemandLen);
				frameBundleBodyCurrLen += bodyDemandLen;

				frameBundleState = FS_BODY_READY;
				return 0;// read body ok!!			
			}else{
				src.get(frameBundleBody,frameBundleBodyCurrLen,maxdatalen);
				frameBundleBodyCurrLen += maxdatalen;		
				return -1;// we want more...	
			}
		}

}
